package handler

import (
	"context"
	"github.com/asim/go-micro/plugins/wrapper/trace/opentracing/v4"
	"github.com/gin-gonic/gin"
	"github.com/zhang-jianqiang/api/config"
	tracer "github.com/zhang-jianqiang/api/config/stracer"
	"github.com/zhang-jianqiang/api/model/response"
	"github.com/zhang-jianqiang/user/proto"
	"go-micro.dev/v4"
	"go-micro.dev/v4/client"
	"time"
)

type User struct {
}

var ApiUser = &User{}

func (u *User) GetUserInfo(c *gin.Context) {
	service := micro.NewService(
		micro.Registry(config.NewNacosRegistry().Register()),
		micro.WrapClient(opentracing.NewClientWrapper(tracer.JaegerTracer)),
	)
	service.Init()
	userService := proto.NewUserService("user", service.Client())
	info, err := userService.Info(context.TODO(), &proto.InfoRequest{
		Username: "18232587802",
	}, func(options *client.CallOptions) {
		// 设置请求超时时间
		options.RequestTimeout = time.Second
	})
	if err != nil {
		RestFailMsg(c, err.Error())
		return
	}
	res := response.UserInfoResponse{
		Username: info.Username,
		Realname: info.Realname,
		Uid:      info.Uid,
		Sex:      info.Sex,
	}
	RestData(c, res)
}
